package com.gxuwz.medical.domain.policy;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.exception.UserNotFoundException;

/**
 * 慢性病政策设置
 * 
 * @author 演示
 * 
 */
public class Policy {

	private String pyear;
	private Double plimit;
	private String rate;
	private String status;
	 
	public Policy() {
	}
	
	public Policy(String pyear)throws UserNotFoundException{
		load(pyear);
	}


	public String getPyear() {
		return pyear;
	}

	public void setPyear(String pyear) {
		this.pyear = pyear;
	}

	public Double getPlimit() {
		return plimit;
	}

	public void setPlimit(Double plimit) {
		this.plimit = plimit;
	}

	

	public Policy(String pyear, Double plimit, String rate, String status) {
		super();
		this.pyear = pyear;
		this.plimit = plimit;
		this.rate = rate;
		this.status = status;
	}

	
	public String getRate() {
		return rate;
	}

	public void setRate(String rate) {
		this.rate = rate;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public void addPolicy()
			throws SQLException {
		Connection conn = null;
		try {
			
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			saveToDB(conn);
			conn.commit();

		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	protected void saveToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into t_policy(pyear,plimit,rate,status)values(?,?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = conn.prepareStatement(sql);
		
			int index = 1;
			pstmt.setString(index++, this.pyear);
			pstmt.setDouble(index++, this.plimit);
			pstmt.setString(index++, this.rate);
			pstmt.setString(index++, this.status);
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			if (pstmt != null) {
				pstmt.close();
			}

		}
	}
	
	public void editPolicy()
			throws SQLException {
		Connection conn = null;
		try {

			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			editToDB(conn);
			conn.commit();

		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	private void editToDB(Connection conn)throws SQLException{
		PreparedStatement pstmt = null;
//		System.out.println("pyear:"+pyear);
//		System.out.println("rate:"+rate);
//		System.out.println("plimit:"+plimit);
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"update t_policy t set t.plimit=?,rate=?,status=? where t.pyear=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setDouble(1, this.plimit);
			pstmt.setString(2, this.rate);
			pstmt.setString(3, this.status);
			pstmt.setString(4, this.pyear);
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("Failed to update t_chronic  !", e);
		} finally {
			DbUtil.close(pstmt);
		}
	}
	public void delPolicy(String pyear)throws SQLException{
		Connection conn = null;
		try{
		this.pyear = pyear;
		conn = DbUtil.getConn();
		conn.setAutoCommit(false);
		delForDB(conn);
		conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
		
	}
	
	protected void delForDB(Connection conn)throws SQLException{
		PreparedStatement pstmt = null;
		try{
			StringBuffer sqlBuffer = new StringBuffer();
			sqlBuffer.append("delete from t_policy where pyear=?");
			pstmt = conn.prepareStatement(sqlBuffer.toString());
			pstmt.setString(1, this.pyear);
			pstmt.executeUpdate();
		}catch(SQLException e){
			throw new SQLException("删除t_policy时出错！");
		}finally{
			DbUtil.close(pstmt);
		}
	}
	
	private void load(String pyear) throws UserNotFoundException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
	//	System.out.println("pyear:"+pyear);
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from t_policy where pyear=?");
			int index = 1;
			pstmt.setString(index,pyear);
			rs = pstmt.executeQuery();
			if (rs != null && rs.next()) {
				this.pyear = rs.getString("pyear");
				this.plimit  = rs.getDouble("plimit");
				this.rate = rs.getString("rate");
				this.status = rs.getString("status");
			}

		} catch (SQLException e) {
			throw new UserNotFoundException("Policy with id: " + pyear
					+ " could not be loaded from the database.");
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}

}
